package com.nicy.dream.biz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 调用接口测试控制器
 * @projectName:dream
 * @see:com.nicy.dream.biz
 * @author:NicyYe
 * @createTime:2021/3/10 15:43
 * @version:1.0
 */
@Api(tags = "uniRest控制器")
@CrossOrigin
@RestController
@RequestMapping("/rest")
public class UniRestController {

    @ApiOperation(value = "获取token方法")
    @PostMapping("/getToken")
    public Map<String,String> getToken(HttpServletRequest request){
        System.out.println(request.getParameter("foo"));
        System.out.println(request.getHeader("accept"));
        System.out.println(request.getParameter("apiKey"));
        //创建map容器
        Map<String,String> map = new HashMap<>();
        //添加token参数
        map.put("token","cf3fc970-8d9a-405a-a7b0-724ea3d0365e");
        //返回值
        return map;
    }

    @ApiOperation(value = "获取base64数据")
    @GetMapping("/base64")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户账号",dataType = "String",required = true),
            @ApiImplicitParam(name = "pwd",value = "密码",dataType = "String",required = true)
    })
    public Map<String,String> getBase64(@RequestParam(name = "user",required = true) String user,
                                        @RequestParam(name = "pwd",required = true) String pwd,HttpServletRequest request){

        //打印原始数据
        System.out.println("-------------------"+user+"-----------------");
        System.out.println("----------base64加密后：----------"+ Base64.getEncoder().encodeToString(user.getBytes(StandardCharsets.UTF_8)));

        System.out.println("--------------------"+pwd+"-----------------");
        System.out.println("----------base64加密后：----------"+ Base64.getEncoder().encodeToString(pwd.getBytes(StandardCharsets.UTF_8)));

        System.out.println(request);

        return null;
    }


    @ApiOperation(value = "查询用户")
    @GetMapping(value = "/queryUser/{fruit}")
    @ApiImplicitParam(name = "fruit",value = "水果",required = true)
    public Map<String,String> queryUser(HttpServletRequest request,@PathVariable(value = "fruit")String fruit){
        //打印参数
        System.out.println(request.getHeader("Accept"));
        System.out.println(request.getHeader("x-custom-header"));

        Map<String,String> result = new HashMap<>();
        System.out.println("**************"+fruit+"**********");
        result.put("name",fruit);
        result.put("hello","luBan");
        //返回值
        return result;
    }

}
